#!/usr/bin/env python
# -*- coding: utf-8 -*- 
############################################################################
#    Copyright (C) 2007-2008
#                  Ferraro Luciano (aka lux) <luciano.ferraro@gmail.com>
#                  Matto Scacco (aka checkm, scaccom) <matto.scacco@gmail.com>
#
#
#    This program is free software; you can redistribute it and/or modify 
#    it under the terms of the GNU General Public License as published by 
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the
#    Free Software Foundation, Inc.,
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
############################################################################

import gtk
import pango

class MessagesBuffer (gtk.TextBuffer):
    def __init__ (self, scrolledwindow):
        gtk.TextBuffer.__init__ (self)
        self.scrolledwindow = scrolledwindow
        
        self.bold = self.create_tag(weight=pango.WEIGHT_BOLD)
        self.underline = self.create_tag(underline=pango.UNDERLINE_SINGLE)
    
    def insert_text (self, action, text, colour=None, weight=None):
        end_iter = self.get_end_iter()
        iter = end_iter
        if (colour == None) and (weight == None):
            self.insert(end_iter, text)# + '\n')
        else:
            tag = self.create_tag()
            if colour: tag.set_property('foreground', colour)
            if weight: tag.set_property('weight', weight)
            self.insert_with_tags(end_iter, text, tag)
        
        #\x034 colore
        #\x02 grassetto
        #\x1f under
        codes = {"\x02":self.bold, "\x034":self.bold, "\x1f":self.underline}
        has_codes = False
        for code in codes.keys():
            if text.find(code) != -1: has_codes = True
        if has_codes:
            print [text]
            for code in codes.keys():
                search = end_iter.backward_search(code, gtk.TEXT_SEARCH_TEXT_ONLY)
                if search:
                    self.apply_tag(codes[code], search[1], end_iter)
                    self.delete(search[0], search[1])
            """if indexes[0]:
                self.apply_tag(self.bold, indexes[0][1], end_iter)
            if indexes[1]:
                self.apply_tag(self.bold, indexes[1][1], end_iter)
            if indexes[2]:
                self.apply_tag(self.underline, indexes[2][1], end_iter)
            
            if indexes[0]: self.delete(indexes[0][0], indexes[0][1])
            if indexes[1]: self.delete(indexes[1][0], indexes[1][1])
            if indexes[2]: self.delete(indexes[1][0], indexes[1][1])"""
            
            #self.insert(end_iter, text[:index])
            #self.insert_with_tags(end_iter, text[index+1:])
        
        self.scrolledwindow.emit("scroll-child", gtk.SCROLL_END, None)
    
class MessagesView (gtk.TextView):
    def __init__ (self, scrolledwindow):
        gtk.TextView.__init__ (self)
        self.modify_base(gtk.STATE_NORMAL, gtk.gdk.color_parse("#EEEEEE"))
        
        self.buffer = MessagesBuffer(scrolledwindow)
        self.font_desc = pango.FontDescription("Monospace 12")
        self.set_buffer(self.buffer)
        self.modify_font(self.font_desc)
        self.set_cursor_visible(False)
        self.set_editable(False)
        self.set_wrap_mode(gtk.WRAP_WORD)
        #self.set_sensitive(False)

class MessagesShow (gtk.ScrolledWindow):
    def __init__ (self):
        gtk.ScrolledWindow.__init__ (self)
        
        self.view = MessagesView(self)
        
        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.add(self.view)

class MessagesEntry (gtk.Entry):
    def __init__ (self):
        gtk.Entry.__init__ (self)
